package com.yilin.tms.core.commons.zysdk;


import com.sun.org.apache.xml.internal.security.utils.Base64;
import com.yilin.commons.exception.RequestLimitExceededException;
import com.yilin.commons.exception.UnauthorizedException;

import java.io.File;
import java.nio.file.Files;
import java.util.Iterator;
import java.util.Map;
import java.util.logging.Logger;

public class DataExchangeService {
    private static final String ACCESS_METHOD_GET = "GET";
    private static final String ACCESS_METHOD_POST = "POST";
    private int CONNECT_TIMEOUT = 360000;
    private int READ_TIMEOUT = 360000;
    protected static final Logger log = Logger.getLogger("com.openapi.sdk.service.DataExchangeService");

    public DataExchangeService(int connectTimeout, int readTimeout) throws RequestLimitExceededException, UnauthorizedException, IllegalArgumentException, Exception {
        this.CONNECT_TIMEOUT = connectTimeout > 0 ? connectTimeout : 120000;
        this.READ_TIMEOUT = readTimeout > 0 ? readTimeout : 120000;
    }

    public String accessHttps(String url, String method) throws RequestLimitExceededException, UnauthorizedException, IllegalArgumentException, Exception {
        if (url != null && url.length() != 0 && method != null && method.trim().length() != 0) {
            if (!"GET".equals(method.toUpperCase()) && !"POST".equals(method.toUpperCase())) {
                throw new IllegalArgumentException("method is only be POST or GET");
            } else {
                String res = HttpsUtils.doPost(url, this.CONNECT_TIMEOUT, this.READ_TIMEOUT);
                return res;
            }
        } else {
            throw new IllegalArgumentException("url or method is required");
        }
    }

    public String postHttps(String url, Map<String, String> param) throws Exception {
        if (url != null && url.length() != 0 && param != null && !param.isEmpty()) {
            String secretKey = param.get("srt");
            if (secretKey != null && !"null".equals(secretKey.trim()) && !"".equals(secretKey.trim())) {
                String clientId = param.get("cid");
                if (clientId != null && !"null".equals(clientId.trim()) && !"".equals(clientId.trim())) {
                    if (param.containsKey("path")) {
                        this.getFileData(param, "path", "fname", "data");
                        this.getFileData(param, "path2", "fname2", "data2");
                        this.getFileData(param, "path3", "fname3", "data3");
                    }
                    param.remove("srt");
                    String sign = SignCommonUtil.signWithParamsOnly(param, secretKey);
                    param.put("sign", sign);
                    System.out.println("sign:" + sign);
                    return HttpsUtils.doPost(url, this.assemeblyPostData(param), this.CONNECT_TIMEOUT, this.READ_TIMEOUT);
                } else {
                    throw new IllegalArgumentException("cid is required");
                }
            } else {
                throw new IllegalArgumentException("srt is required");
            }
        } else {
            throw new IllegalArgumentException("url or method is required");
        }
    }

    private void getFileData(Map<String, String> param, String fileName, String fname, String fdata) throws Exception {
        try {
            String path = param.get(fileName);
            File file = null;
            if ("path".equals(fileName)) {
                if (path == null || "null".equals(path.trim()) || "".equals(path.trim())) {
                    throw new IllegalArgumentException("path is required");
                }

                file = new File(path);
            } else if (path != null && !"null".equals(path.trim()) && !"".equals(path.trim())) {
                file = new File(path);
            }

            if (file != null) {
                param.remove(fileName);
                long length = file.length();
                if (length > 5242880L) {
                    throw new IllegalArgumentException("file size is over limit 5M");
                } else {
                    byte[] bFile = Files.readAllBytes((new File(path)).toPath());
                    String data = Base64.encode(bFile);
                    param.put(fname, file.getName());
                    param.put(fdata, data);
                }
            }
        } catch (Exception var11) {
            System.out.println(var11.getMessage() + ",e:" + var11.toString());
            throw var11;
        }
    }

    private String assemeblyPostData(Map<String, String> param) {
        StringBuffer buffer = new StringBuffer(200);
        if (param != null && !param.isEmpty()) {
            Iterator var4 = param.entrySet().iterator();

            while(var4.hasNext()) {
                Map.Entry<String, String> entry = (Map.Entry)var4.next();
                buffer.append((String)entry.getKey()).append("=").append((String)entry.getValue()).append("&");
            }

            buffer.deleteCharAt(buffer.length() - 1);
        }

        return buffer.toString();
    }
}
